/*
 * @Author: CPS
 * @Date: 2020-07-25 18:01:11
 * @Last Modified by:   Administrator
 * @Last Modified time: 2020-08-03 17:17:00
 * @Projectname Electron-fileWatcher
 * @Filename index.js
 */

'use strict'
import smallNoification from './smallNoification.pug'
import './smallNoification.stylus'

// import windownoification from './noificationToWindow.pug'
// import './noificationToWindow.stylus'

import iconClose from '!!svg-inline-loader!./icon/u90.svg'
import iconFolder from '!!svg-inline-loader!./icon/u90.svg'

let icon = {
  svg: {
    '关闭按钮': iconClose,
    '文件夹': iconFolder
  }
}

class notifiObject {
  constructor(target) {
    this.smallNotis = smallNoification
    // this.windowNotis = windownoification
    this.settings = {
      icon: icon,
      msg: {
        title: '(161332) 翁瑞珠 打印3本',
        content: '文件夹被创建了!!!'
      }, //消息内容
      max: 3, //最大同时存在的通知数量
      dir: 0, //通知生成的方向
      delay: 6000, //消失时间
    }
    this.el = null
    this.history = []
    this.id = 0

    if (target) {
      switch (target.toString()) {
        case '[object HTMLElement]':
          //传入的时一个element类型
          this.el = target
          break;

        default:
          let tmp = document.getElementById(target);
          if (tmp) {
            this.el = tmp
          }
          break;
      }
    } else {
      this.el = document.createElement('section')
      this.el.id = this.el.className = 'smallNoification__component'
    }

    this.el.addEventListener('click', (e) => {
      switch (e.target.className) {
        case 'smallNoification__btn-close':
          console.log('点击了关闭按钮')
          this.el.removeChild(e.target.parentNode)
          break;
        default:
          console.log('我被点击了:' + e.target.className)
          break;
      }
    })

    return this
  }

  pushMsg(newSettings) {
    Object.assign(this.settings, newSettings)

    // 超过指定数量,进行删减
    if (this.el.childElementCount >= this.settings.max) {
      if (this.settings.dir) {
        this.el.removeChild(this.el.firstElementChild)
      } else {
        this.el.removeChild(this.el.lastElementChild)
      }
    }

    // 将消息添加到历史记录
    if (this.settings.msg) {
      this.history.push(this.settings.msg.toString())
    }

    let tar
    // 根据不同方向,生成标签
    if (this.settings.dir) {
      this.el.innerHTML = this.el.innerHTML + this.smallNotis(this.settings)
    } else {
      this.el.innerHTML = this.smallNotis(this.settings) + this.el.innerHTML
    }

    this.delMsgInterval(this.el)
  }

  delMsgInterval(tar) {
    if (this.id) return //不重复添加监听器

    //设置每隔一段事件删除旧的通知
    this.id = setInterval(() => {
      if (!tar.childElementCount > 0) {
        clearInterval(this.id)
        this.id = 0
      } else {
        tar.lastElementChild.style.opacity = 0
        setTimeout(() => {
          tar.removeChild(tar.lastElementChild)
        }, 1000, tar)
      }
    }, this.settings.delay, tar)
  }
}

export default (target) => {
  return new notifiObject(target)
}
